home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-24 | 7.1 KB | 297 lines | [TEXT/CWIE] |
- // Dialog library, created by Resorcerer and modified by RDC
-
- /*
- * Center a given window, w, horizontally in the main screen, top pixels from
- * the top, or centered vertically if top is 0. The window should be invisible.
- */
-
- #include "ResorcererDialogLib.h"
-
-
- void CenterWindow(WindowPtr w, int top)
- {
- Rect scr; Point p;
- int rsize,size,margin,xoff,yoff;
-
- scr = qd.screenBits.bounds;
- SetPort(w);
- p.h = w->portRect.left; p.v = w->portRect.top;
- LocalToGlobal(&p);
-
- size = scr.right - scr.left;
- rsize = w->portRect.right - w->portRect.left;
- margin = size - rsize;
- if (margin > 0) {
- margin >>= 1;
- p.h = scr.left + margin;
- }
- size = scr.bottom - scr.top;
- rsize = w->portRect.bottom - w->portRect.top;
- margin = size - rsize;
- if (margin > 0) {
- margin >>= 1;
- p.v = scr.top + margin;
- }
- MoveWindow(w,p.h,top?top:p.v,FALSE);
- }
-
- /* Local C string length routine */
-
- static long strlen(register char *str)
- {
- register char *p;
-
- p = str;
- while (*p++) ;
- return((long)(--p - str));
- }
-
- /* Convert in place a Pascal string to C string, and deliver its address */
-
- char *PascalToC(char *str)
- {
- register char *p,*q,*end;
-
- end = str + *(unsigned char *)str;
- q = (p=str) + 1;
- while (p < end) *p++ = *q++;
- *p = '\0';
- return(str);
- }
-
- /*
- * Convert in place a C string to Pascal string, and deliver its address.
- * The C string should not be greater than 255 chars in length, or the
- * resulting Pascal string will be truncated to 255 chars.
- */
-
- char *CToPascal(char *str)
- {
- register char *p,*q;
- register long len;
-
- len = strlen(str);
- if (len > 255) len = 255;
- p = str + len;
- q = p-1;
- while (p != str) *p-- = *q--;
- *str = len;
- return(str);
- }
-
- /* Dialog Item Stuffers */
-
- /*
- * Install a given Pascal string, str, into the given static or edit text item
- * in the dialog, dlog. If the item is an edit text item, leave the installed
- * text selected or not according to the value of sel (TRUE or FALSE).
- */
-
- void PutDlgString(DialogPtr dlog, int item, char *str, int sel)
- {
- short type; Handle hndl; Rect box;
-
- GetDItem(dlog,item,&type,&hndl,&box);
- SetIText(hndl,(void*)str);
- if (type == editText)
- SelIText(dlog,item,sel?0:32767,32767);
- InvalRect(&box);
- }
-
- /*
- * Install a given decimal long value into the static or edit text item of the
- * given dialog, dlog. If the item is an edit text item, leave the installed
- * text for the number selected or not according to sel (TRUE or FALSE).
- */
-
- void PutDlgLong(DialogPtr dlog, int item, long val, int sel)
- {
- char str[32];
-
- NumToString(val,(void*)str);
- PutDlgString(dlog,item,str,sel);
- }
-
- /*
- * Same as above, only for an int (word) decimal number.
- */
-
- void PutDlgWord(DialogPtr dlog, int item, int val, int sel)
- {
- PutDlgLong(dlog,item,(long)val,sel);
- }
-
- /*
- * Set the given check box or radio button item of the given dialog, dlog, to
- * on or off, according to val.
- */
-
- void PutDlgChkRadio(DialogPtr dlog, int item, int val)
- {
- short type; Handle hndl; Rect box;
-
- GetDItem(dlog,item,&type,&hndl,&box);
- SetCtlValue((ControlHandle)hndl,val!=0);
- }
-
- /*
- * Deliver the value of the checkbox or radio button item of the given dialog.
- */
-
- int GetDlgChkRadio(DialogPtr dlog, int item)
- {
- short type; Handle hndl; Rect box;
-
- GetDItem(dlog,item,&type,&hndl,&box);
- return(GetCtlValue((ControlHandle)hndl) != 0);
- }
-
- /* Dialog Item Unstuffers */
-
- /*
- * Retrieve the value of an edit text item in a given dialog, placing the
- * resulting Pascal string in the buffer, str, which is assumed large enough
- * to hold the text (256 bytes max). If item is the number of a static text
- * item, the empty string is delivered. Delivers TRUE or FALSE according to
- * whether or not the text so delivered was empty.
- */
-
- int GetDlgString(DialogPtr dlog, int item, char *str)
- {
- short type; Handle hndl; Rect box;
-
- GetDItem(dlog,item,&type,&hndl,&box);
- if (type == editText) GetIText(hndl,(void*)str);
- else *str = 0;
- return(*str != 0);
- }
-
- /*
- * Retrieve the value of an edit text item in a given dialog, converting the
- * Pascal string to a long and setting *val to it. Delivers TRUE or FALSE
- * according to whether or not the text so delivered was empty. If FALSE,
- * *val is set to 0; if TRUE, *val is set to whatever StringToNum() says the
- * value is, even if the text contains non-numerical characters.
- */
-
- int GetDlgLong(DialogPtr dlog, int item, long *val)
- {
- int ans; char str[256];
-
- *val = 0;
- if (ans = GetDlgString(dlog,item,str)) {
- // Check that we do indeed have a valid number
- int count;
-
- for (count = *str; count >= 1; count--) {
- char currChar = str[count];
- if ((currChar < '0') || (currChar > '9'))
- return false;
- }
- StringToNum((void*)str,val);
- }
- return(ans);
- }
-
- /* Same as above, only delivers the value of a word */
-
- int GetDlgWord(DialogPtr dlog, int item, short *val)
- {
- int ans; long num;
-
- *val = 0;
- if (ans = GetDlgLong(dlog,item,&num))
- *val = num;
- return(ans);
- }
-
- /*
- * Deliver the number of the current editText item in given dialog if any text
- * is selected in it, or 0 if none selected.
- */
-
- int TextSelected(DialogPtr dlog)
- {
- register TEHandle textH; int item = 0;
-
- textH = ((DialogPeek)dlog)->textH;
- if (*textH)
- if ( (*textH)->selStart != (*textH)->selEnd )
- item = ((DialogPeek)dlog)->editField+1;
- return(item);
- }
-
- /*
- * If any of the variable argument scrap types are available for pasting from
- * the scrap, deliver the first one. Otherwise, deliver 0. For example,
- *
- * if (whichType = CanPaste(3,'TEXT','PICT','STUF')) ...
- *
- * There can be any number of types in the list, as long as the preceding count, n,
- * is correct.
- */
-
- OSType CanPaste(int n, ...)
- {
- register OSType nextType,ans = 0L;
- long err,offset;
- va_list nextArg;
-
- va_start(nextArg,n);
- nextType = va_arg(nextArg, OSType);
-
- while (n-- > 0) {
- err = GetScrap(NIL, nextType, &offset);
- if (err >= -1) {
- ans = nextType;
- break;
- }
- nextType = va_arg(nextArg, OSType);
- }
-
- va_end(nextArg);
- return(ans);
- }
-
- /*
- * Frame or unframe a default dialog item (presumed a button) in given dialog.
- * Note that you don't need to use an extra user item to do this unless you
- * are doing some sort of non-standard default highlighting (not recommended).
- */
-
- void FrameDefault(DialogPtr dlog, int item, int frame)
- {
- short type; Handle hndl; Rect box;
- GrafPtr oldPort; PenState oldPen;
-
- GetPort(&oldPort); SetPort(dlog);
- GetPenState(&oldPen);
-
- GetDItem(dlog,item,&type,&hndl,&box);
- InsetRect(&box,-4,-4);
-
- PenSize(3,3);
- if (frame) PenPat(&qd.black); /* Paint frame */
- else PenPat(&qd.white); /* Erase frame */
- FrameRoundRect(&box,16,16);
-
- SetPenState(&oldPen);
- SetPort(oldPort);
- }
-
- /*
- * Get rectangle, *panel, for a given item (usually a user or picture item)
- * and then hide the item so that it doesn't interfere with mouse clicking.
- * This lets you stop worrying about the item order any user or pict items that
- * obscure other items in the item list, which can affect how the DialogMgr
- * returns itemHits.
- */
-
- void GetDlgPanel(DialogPtr dlog, int item, Rect *panel)
- {
- short type; Handle hndl;
-
- GetDItem(dlog,item,&type,&hndl,panel);
- HideDItem(dlog,item);
- }
-